const { body } = require("express-validator");
const validate = require("../middleware/validate");

// 基础数据校验
exports.register = validate(
  [
    body("nickname")
      .notEmpty()
      .withMessage("匿名不能为空")
      .isLength({ min: 4 })
      .withMessage("匿名必须是4位以上字符"),
    body("password")
      .notEmpty()
      .withMessage("密码不能为空")
      .isLength({ min: 6, max: 16 })
      .withMessage("密码必须是6-16为字符"),
    // body("email").isEmail(),
    body("phone")
      .notEmpty()
      .withMessage("手机号码不能为空")
      .bail()
      .custom(async (phone) => {
        var re = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
        var result = re.test(phone);
        if (!result) {
          return Promise.reject("手机格式不正确");
        }
      }),
  ],
  async (req, res, next) => {
    next();
  }
);

exports.loginByName = validate(
  [
    body("nickname")
      .notEmpty()
      .withMessage("用户名不能为空")
      .isLength({ min: 4 })
      .withMessage("用户名少于4位字符"),
    body("password")
      .notEmpty()
      .withMessage("密码不能为空")
      .isLength({ min: 6, max: 16 })
      .withMessage("密码必须是6-16为字符"),
    // body("email").isEmail(),
  ],
  async (req, res, next) => {
    next();
  }
);
